<?php
namespace app\shell;

use share\console\CConsole;
use share\console\CLIRunner;
use share\exception\ApiException;
use share\exception\Errno;
use share\service\MQService;
use think\Log;

class CMqBack  extends CLIRunner{

    public function call_by_cache_key_action($cacheKey){
        $params = MQService::getMQData($cacheKey);
        // trace("call_by_cache_key {$cacheKey}: params:".json_encode($params),Log::ERROR);
        // 删除原始数据
        MQService::delMQDataCache($cacheKey);

        if(empty($params)){
            trace("mq.call_by_cache_key token ".$cacheKey." data lost!",Log::ERROR);
            return;
        }
        if(!isset($params['service']) || !isset($params['params'])){
            trace("mq.call_by_cache_key token ".$cacheKey." data lost params. data:".json_encode($params,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES),Log::ERROR);
            return;
        }
        $service = $params['service'];
        $serviceInfo = explode('@',$service);
        $class = $serviceInfo[0];
        $method = $serviceInfo[1];
        $args = $params['params'];
        return $this->call_by_params_action($class,$method,$args);
    }

    public function call_by_params_action($service,$method,$params){
        //trace("mq.call_by_params now do call_by_params {$service}@{$method} params:".json_encode($params,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES),Log::ERROR);
        if(!method_exists($service,$method)){            
            trace("mq.call_by_params service ".$service." not exist!",Log::ERROR);
            return ;
        }
        $res = null;
        if(isset($params[0])){ //数组形式的参数
            $res = call_user_func_array([$service,$method],$params);
        }
        else{
            $res = call_user_func([$service,$method],$params);
        }
        return $res;
    }

    public function test_action(){
        $data = ['test'=>'test123'];
        $token = MQService::saveMQData($data);
        CConsole::print_line("token is ".$token);
        return MQService::getMQData($token);
    }

    public function test_shell_action(){
        $command = "nohup php F:\\data\\wwwroot\\enz-api-rebuild\\account\\cli.php CMqBack sleep 2>&1 &";
        $fp = popen($command,'r');
        return $command;
    }


    public function sleep_action(){
        $times = 10;
        while($times > 0){
            sleep(1);
            trace("sleep ".$times,Log::ERROR);
            CConsole::print_line("sleep ".$times);
            $times--;
        }
    }

    public function send_sleep_test_action(){
        $times = mt_rand(5,10);
        MQService::push([
            'service' => 'app\\shell\\CMqBack@sleep_test',
            'params' => [
                $times
            ],
        ],MQService::CHANNEL_OF_ACCOUNT);
    }


    public static function sleep_test($times){
        while($times > 0){
            sleep(1);
            trace("sleep ".$times,Log::ERROR);
            CConsole::print_line("sleep ".$times);
            $times--;
        }
    }

    public static function test_error_action(){
        throw new ApiException("脚本执行推送测试",Errno::COMMON_SYSTEM_ERROR);
    }
}